//分类数据
import { getCategoryAPI } from '@/apis/category'
import { ref } from 'vue';
import { onMounted } from 'vue';
import { onBeforeRouteUpdate, useRoute } from 'vue-router';



export function useCategory() {
    const route = useRoute()
const categoryData = ref({})

//默认参数为路由参数的id
const getCategory = async (id=route.params.id) => {
  const res = await getCategoryAPI(id)
  categoryData.value = res.result
}
onMounted(() => {
  getCategory()
 
})


// onUpdated(() => {
//   getCategory()
// })

//改用路由守卫 （路由更新前 所以要解决滞后性)  形参为跳转目的
onBeforeRouteUpdate((to) => {
  getCategory(to.params.id)
})
    
    return { categoryData }
}